/**
 * Copyright &copy; 2013-2016 山东易科德软件有限公司 All rights reserved.
 */
package cn.net.ecode.modules.cms.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.net.ecode.common.persistence.Page;
import cn.net.ecode.common.service.CrudService;
import cn.net.ecode.modules.cms.dao.SiteDao;
import cn.net.ecode.modules.cms.entity.Article;
import cn.net.ecode.modules.cms.entity.Category;
import cn.net.ecode.modules.cms.entity.Site;
import cn.net.ecode.modules.cms.utils.CmsUtils;

/**
 * 站点Service
 * @author ThinkGem
 * @version 2014-9-16
 */
@Service
@Transactional(readOnly = true)
public class SiteService extends CrudService<SiteDao, Site> {
	
	@Autowired
	private ArticleService articleService;
	@Autowired
	private LinkService linkService;
	
	/**
	 * 获取单条数据
	 * @param site
	 * @return
	 */
	public Site get(Site site) {
		return super.get(site);
	}
	
	/**
	 * 查询列表数据
	 * @param site
	 * @return
	 */
	public List<Site> findList(Site site) {
		return super.findList(site);
	}
	
	/**
	 * 查询分页数据
	 * @param page 分页对象
	 * @param site
	 * @return
	 */
	public Page<Site> findPage(Page<Site> page, Site site) {
		return super.findPage(page, site);
	}

	/**
	 * 保存站点
	 */
	@Transactional(readOnly = false)
	public void save(Site site) {
		if (site.getIsNewRecord()){
			//genIdAndValid(site, site.getViewCode());
		}
//		if (site.getCopyright()!=null){
//			site.setCopyright(StringEscapeUtils.unescapeHtml4(site.getCopyright()));
//		}
		super.save(site);
		CmsUtils.removeCache("siteList");
	}
	
	/**
	 * 删除站点
	 * @param site
	 * @param isRe
	 */
	@Transactional(readOnly = false)
	public void delete(Site site, Boolean isRe) {
		site.setStatus(isRe != null && isRe ? Site.STATUS_NORMAL : Site.STATUS_DELETE);
		super.delete(site);
		CmsUtils.removeCache("siteList");
		
		// 根据站点重建索引
		rebulidIndex(site, true, false);
	}
	
	/**
	 * 根据站点重建索引
	 */
	public void rebulidIndex(Site site, boolean isDelete, boolean isUpdate){
		// 根据站点重建索引
		Article article = new Article(new Category());
		article.getCategory().setSite(new Site(site.getSiteCode()));
	}
	
}
